package com.lib.framework.base.mvvm

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.lib.framework.base.BaseActivity
import com.lib.framework.entity.Event
import com.lib.framework.entity.StartActivityInfo
import com.lib.framework.ext.observeForeverNotException


/**
 * 带 ViewModel 的 Activity 的基类
 *
 * @author keanbin
 */
abstract class BaseViewModelActivity<T : BaseActivityViewModel> : BaseActivity() {

    companion object {

        /**
         * 事件标记范围 99800 ~ 99899
         */
        //const val EVENT_XXXX = 99800 // XXXX事件
    }

    /**
     * 该页面的ViewModel， 延时初始化，在onCreate 中初始化
     */
    lateinit var mViewModel: T

    /**
     * 该页面的事件 LiveData， 延时初始化，在onCreate 中初始化
     */
    private lateinit var mEventLiveData: LiveData<Event>

    /**
     * 获取 ViewModel 的类
     */
    abstract fun getViewModelClass(): Class<T>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mViewModel = ViewModelProviders.of(this).get(getViewModelClass())

        mEventLiveData = mViewModel.getEventLiveData()
        mEventLiveData.observeForeverNotException(mEventObserver)

        mViewModel.initData(intent)
    }

    private val mEventObserver = Observer<Event> {
        dealEvent(it)
    }

    /**
     * 处理事件
     */
    open fun dealEvent(event: Event?): Boolean {
        when (event?.code) {
            BaseViewModel.EVENT_FINISH -> { // 关闭页面事件
                finish()
                return true
            }

            BaseViewModel.EVENT_PROMPT_TEXT -> { // 显示提示文本对话框事件
                val text = event.extraObj as? String
                showPrompt(text)
                return true
            }

            BaseViewModel.EVENT_SHOW_LOADING_DIALOG -> { // 显示Loading对话框事件
                val isCancelable = event.extraObj as? Boolean ?: false
                val text = event.extras.getString(BaseViewModel.KEY_TEXT)
                showLoadingDialog(isCancelable, text)
                return true
            }

            BaseViewModel.EVENT_CLOSE_LOADING_DIALOG -> { // 关闭Loading对话框事件
                closeLoadingDialog()
                return true
            }

            BaseViewModel.EVENT_START_ACTIVITY -> { // 启动页面事件
                val startActivityInfo = event.extraObj as? StartActivityInfo
                startActivityInfo?.let {
                    val requestCode = it.requestCode
                    if (requestCode == null) {
                        startActivity(it.intent)
                    } else {
                        startActivityForResult(it.intent, requestCode)
                    }

                    if (it.isSetResultOk) {
                        setResult(Activity.RESULT_OK)
                    }

                    if (it.finishActivity) {
                        finish()
                    }
                }
                return true
            }

            BaseViewModel.EVENT_SET_RESULT_OK -> { // 设置 RESULT_OK
                val intent = event.extraObj as? Intent
                setResult(Activity.RESULT_OK, intent)
                return true
            }
        }

        return false
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        mViewModel.onActivityResult(requestCode, resultCode, data)
    }

    override fun onDestroy() {
        super.onDestroy()

        mEventLiveData.removeObserver(mEventObserver)
    }
}